package com.example.administrator.nurse.widget;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;

/**
 * Created by Administrator on 2016/5/8.
 */
public class CommonViewPagerHelper {
    private ViewPager viewPager;
    private ViewPager.OnPageChangeListener onPageChangeListener;
    private PagerAdapter pagerAdapter;
    private int length;
    public CommonViewPagerHelper(ViewPager viewPager){
        this.viewPager=viewPager;
    }
    public CommonViewPagerHelper withAdapter(PagerAdapter pagerAdapter){
        this.pagerAdapter=pagerAdapter;
        return this;
    }
    public CommonViewPagerHelper withOnPageChangeListener(ViewPager.OnPageChangeListener onPageChangeListener){
        this.onPageChangeListener=onPageChangeListener;
        return this;
    }
    public CommonViewPagerHelper withListLength(int length){
        this.length=length;
        return this;
    }
    public void initHelper(){
        if (pagerAdapter!=null){
            this.viewPager.setAdapter(pagerAdapter);
        }
        if (onPageChangeListener!=null){
            this.viewPager.addOnPageChangeListener(onPageChangeListener);
        }
    }
    public void setListItemClick(View ...views) throws ListItemNumberOutofList {
        if (views.length==this.length){
            for (int i=0;i<views.length;i++){
                final int finalI = i;
                views[i].setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(finalI);
                    }
                });
            }
        }else{
            throw new ListItemNumberOutofList("tab项数目超过ViewPager实际列表数量");
        }
    }

    /**
     * 声明当tab项多于实际ViewPager中的列表项数目时触发的异常
     */
    public class ListItemNumberOutofList extends Exception{
        public ListItemNumberOutofList(String  msg){
            super(msg);
        }
    }
}
